<?php
// +----------------------------------------------------------------------
// | HkCms URL规则管理
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2019 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

namespace app\content\controller;

use common\controller\AdminBase;
use app\content\validate\Urlrule as UrlruleValidate;

class Urlrule extends AdminBase
{
	
    //初始化
    public function _initialize() {
        parent::_initialize();
        //可用模块列表
        $Module = array(
            'content' => array(
                'module' => 'content',
                'name' => '内容模块',
            ),
        );
        if (hk_cache('Module')) {
	    	foreach (hk_cache('Module') as $r) {
	            $Module[strtolower($r['module'])] = array(
	                'module' => strtolower($r['module']),
	                'name' => $r['modulename']
	            );
	    	}
        }

        $this->assign('Module', $Module);
    }

    //URL规则显示
    public function index() {

        $this->assign('info', model('content/Urlrule')->order(array('urlruleid' => 'DESC'))->select()->toarray());
        return $this->fetch();
    }

    //添加新规则
    public function add() {

    	if ( request()->isPost() ) {

    		$data = input('post.');

	        $validate = new UrlruleValidate;

	        $result = $validate->check($data);

	        // 验证数据
	        if(!$result){

	            $this->error($validate->getError());

	        }

	        $res = model('admin/Urlrule')->add($data);

    		if( $res ){

    			$this->success('添加成功！',url('Urlrule/index'));

    		}else{

    			$this->error(model('admin/Urlrule')->error);
    		}
    	}

        return $this->fetch();
    }

    //编辑规则
    public function edit() {

    	if ( request()->isPost() ) {

    		$data = input('post.');unset($data['f1']);

    		if (!input('urlruleid')) {
    			
    			$this->error('id不存在');
    		}

	        $validate = new UrlruleValidate;

	        $result = $validate->check($data);

	        // 验证数据
	        if(!$result){

	            $this->error($validate->getError());

	        }

	        $res = model('admin/Urlrule')->edit($data);

    		if( $res ){

    			$this->success('修改成功！',url('Urlrule/index'));

    		}else{

    			$this->error(model('admin/Urlrule')->error);
    		}
    	}

    	$data = model('admin/Urlrule')->get(input('urlruleid'));

    	$this->assign('data',$data);

        return $this->fetch();
    }

    //删除规则
    public function delete() {

    	$urlruleid = input('urlruleid');

    	if ( empty($urlruleid) ) {

    		$this->error('请指定需要修改的信息！');
    	}

        $data = model('admin/Urlrule')->get($urlruleid);

        if (empty($data)) {

            $this->error('该信息不存在！');
        }

        if ( model('admin/Urlrule')->destroy($urlruleid)) {

        	$this->success('删除成功！', url('Urlrule/index') );
        }

        $this->error('删除失败！');
    }

}